import { adList, announcementList, gameRoom,checkToken } from "@/api/index";
import { goodslist } from "@/api/money";
const state = {
    langCode: '',
    bannerListData: [],
    annnounListData: [],
    gameRoomData: [],
    rechargeData: [],
};

const mutations = {
    SET_LANGCODE: (state, langCode) => {
        state.langCode = langCode;
    },
    SET_bannerListData: (state, bannerListData) => {
        state.bannerListData = bannerListData;
    },
    SET_annnounListData: (state, annnounListData) => {
        state.annnounListData = annnounListData;
    },
    SET_gameRoomData: (state, gameRoomData) => {
        state.gameRoomData = gameRoomData;
    },
    SET_rechargeData: (state, rechargeData) => {
        state.rechargeData = rechargeData;
    },
};

const actions = {
    async setlange({ commit },data) {
        // 将获取到的游戏配置信息保存到state中
        commit('SET_LANGCODE', data);
    },
    async homeInitData({ commit,rootState,dispatch }){
        console.log(rootState.user);
        let param = {};
        if (localStorage.getItem("TOKEN")) {
            let param2 = {
              token: localStorage.getItem("TOKEN"),
              tokenUuid: localStorage.getItem("UUIDTOKEN")
            };
            checkToken(param2).then(res => {
              console.log("token", res);
              if (res.data.data.tokenStatus === "FAIL") {
                localStorage.removeItem("TOKEN");
                localStorage.removeItem("UUIDTOKEN");
              }else{
                dispatch('user/getMineInfo', null, { root: true });
              }
            });
          }
        adList(param).then(res => {
            commit('SET_bannerListData', res.data.data.list);
        });
        announcementList(param).then(res => {
            commit('SET_annnounListData', res.data.data.list);
        });
        gameRoom(param).then(res => {
            commit('SET_gameRoomData', res.data.data.list);
        });
        goodslist(param).then(res => {
            commit('SET_rechargeData', res?.data?.data?.list);
        });
    },


};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
